﻿using GalaSoft.MvvmLight;
using WP7Contrib.Collections;
using CDFWPUG.Model;
using System.Linq;
using System.Collections.Generic;
using CDFWPUG.Objects;
using System.Net;
using System;
using Microsoft.Phone.Net.NetworkInformation;

namespace CDFWPUG.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class pageGroupAppsViewModel : ViewModelBase
    {

        



        /// <summary>
        /// Initializes a new instance of the pageGroupAppsViewModel class.
        /// </summary>
        public pageGroupAppsViewModel()
        {

            _apps = new ObservableCollection<UserApp>();
            _GroupedApps = new ObservableCollection<GroupedObservableCollection<UserApp>>();
            if (IsInDesignMode)
            {
                _apps = Helpers.AppHelper.Test_GetFeaturedUserApps(0);
                _GroupedApps = Helpers.AppHelper.GetGroupedUserApps(_apps);
            }
            else
            {
                LoadGroupApps();
                GroupedApps = Helpers.AppHelper.GetGroupedUserApps(_apps);
            }
        }

        public override void Cleanup()
        {
            // Clean own resources if needed

            base.Cleanup();
        }

        #region Properties
        private ObservableCollection<Model.UserApp> _apps;
        public ObservableCollection<Model.UserApp> Apps
        {
            get
            {
                return _apps;
            }
            set
            {
                if (_apps != value)
                {
                    _apps = value;
                    RaisePropertyChanged("Apps");
                }
            }
        }
        private ObservableCollection<Objects.GroupedObservableCollection<UserApp>> _GroupedApps;
        public ObservableCollection<Objects.GroupedObservableCollection<UserApp>> GroupedApps
        {
            get
            {
                return _GroupedApps;
            }
            set
            {
                if (_GroupedApps != value)
                {
                    _GroupedApps = value;
                    RaisePropertyChanged("GroupedApps");
                }
                
            }

        }


        private Boolean _GroupAppsLoading;
        public Boolean GroupAppsLoading
        {
            get { return _GroupAppsLoading; }
            set
            {
                if (_GroupAppsLoading != value)
                {
                    _GroupAppsLoading = value;
                    RaisePropertyChanged("GroupAppsLoading");
                }

            }
        }

        private String _GroupAppsMessage;
        public String GroupAppsMessage
        {
            get { return _GroupAppsMessage; }
            set
            {
                if (_GroupAppsMessage != value)
                {
                    _GroupAppsMessage = value;
                    RaisePropertyChanged("GroupAppsMessage");
                }

            }
        }
#endregion


        public void LoadGroupApps()
            {
                if (Helpers.IsolatedStorageHelper.FileExists("UserApps.xml"))
                {
                    GroupAppsLoading = true;
                    var results = Helpers.AppHelper.GetAppsFromIsolatedStorage();
                    Apps.Clear();
                    foreach (var item in results)
                    {
                        Apps.Add(item);
                    }
                    GroupAppsLoading = false;
                }
                else
                {
                    //no file in isolated storage so download it
                    UpdateGroupApps();
                }
            }

        public void UpdateGroupApps()
            {
                WebClient appsFeed = new WebClient();
                bool hasNetworkConnection = NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None;
                string xmldata = "http://bugail.com/cdfwpug/data/UserApps.xml";

                if (hasNetworkConnection == true)
                {
                    GroupAppsLoading = true;
                    appsFeed.DownloadStringCompleted += new DownloadStringCompletedEventHandler(appsFeed_DownloadStringCompleted);
                    appsFeed.DownloadStringAsync(new Uri(xmldata));
                }
                else
                {
                    GroupAppsMessage  = "There is currently no network coverage, try again later";
                    Apps.Clear();
                    GroupAppsLoading = false;
                    //lblLoading.Visibility = System.Windows.Visibility.Visible;
                }

                //Helpers.IsolatedStorageHelper.SaveFile("meetings.xml", xml);
                //LoadMeetings();
            }

        void appsFeed_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string xml = e.Result;
                if (Helpers.IsolatedStorageHelper.FileExists("UserApps.xml"))
                {
                    Helpers.IsolatedStorageHelper.DeleteFile("UserApps.xml");
                }
                Helpers.IsolatedStorageHelper.SaveFile("UserApps.xml", xml);
                LoadGroupApps();
            }
            else
            {
                GroupAppsMessage = "Opps! There was a problem while getting the apps, please try again later";

            }
            GroupAppsLoading = false;
        }



        


      

       








    }
}